/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;


import Controller.Speaking;
import Controller.SpeakingDAO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Timer;

/**
 *
 * @author ngoc
 */
public class Fspeaking extends javax.swing.JFrame {

    /**
     * Creates new form Fspeaking
     */
    
    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    
    Vector vSpeakingID = new Vector();
    Vector vSpeakingname = new Vector();
    Vector vUrlSpeaking = new Vector();
    Vector vUrlTime = new Vector();
    Vector vUrlHistory = new Vector();
    
    
    SpeakingDAO spDAO = new SpeakingDAO();
    int NumberQuestion;
    String[] arr = null ;
    String[] arrtime = null;
    
    
    boolean start = true;
    int hour = 0 ;
    int min = 0 ;
    int sec = 0 ;
    Timer timer;
    
    public Fspeaking() {
        initComponents();
        
        btnplay.setEnabled(false);
        btnsave.setEnabled(false);
        
        txtQuestion.setEditable(false);
        txtQuestion.setLineWrap(true);
        txtQuestion.setWrapStyleWord(true);
        
        for (Speaking speaking : spDAO.getAll()) {
            vSpeakingID.add(speaking.getSpeakingID());
            vSpeakingname.add(speaking.getSpeakingname());
            vUrlSpeaking.add(speaking.getUrlSpeaking());
            vUrlTime.add(speaking.getUrlTime());
            vUrlHistory.add(speaking.getUrlHistory());
        }
    cbchoose.setModel(new DefaultComboBoxModel(vSpeakingname));
    lbquestion.setVisible(false);
    
        try {
            NumberQuestion = spDAO.countLines((String)vUrlSpeaking.get(cbchoose.getSelectedIndex()));
        } catch (IOException ex) {
            Logger.getLogger(Fspeaking.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        String filename = (String)vUrlSpeaking.get(cbchoose.getSelectedIndex());
    
        try {
            arr = spDAO.readUsingBufferedReader(filename, StandardCharsets.UTF_8);
            txtQuestion.setText(arr[0]);
            lbquestion.setText("0");
        } catch (IOException ex) {
            Logger.getLogger(Fspeaking.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        //time
        String UrlTime = (String)vUrlTime.get(cbchoose.getSelectedIndex());
        
        try {
            arrtime = spDAO.readUsingBufferedReader(UrlTime, StandardCharsets.UTF_8);
                    
                    } catch (IOException ex) {
            Logger.getLogger(Fspeaking.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        String[] parts = arrtime[0].split(":");
        hour =    Integer.parseInt(parts[0]) ;
        min =    Integer.parseInt(parts[1]) ;
        sec =    Integer.parseInt(parts[2]) ;
        lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));
        
        
         timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            
                     
	if(start==true)
	{	
            lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));
        if( hour == 0 && min == 0 && sec == 0)
        {
	start = false;
	lbtime.setText("Hết giờ");
	}
        
        sec-=1;
	if(sec < 0 && (min > 0 || hour > 0))
	{ 
	sec = 59;
	min -= 1;
	}
	if(min < 0 && hour > 0)
	{
	min = 59;
	hour -= 1;
	}
            }
            }
        });
 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btngroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        cbchoose = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnplay = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        btnRecord = new javax.swing.JButton();
        btnnext = new javax.swing.JButton();
        btnback = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtQuestion = new javax.swing.JTextArea();
        lbquestion = new javax.swing.JLabel();
        lbtime = new javax.swing.JLabel();
        rda = new javax.swing.JRadioButton();
        rdb = new javax.swing.JRadioButton();
        rdc = new javax.swing.JRadioButton();
        rdd = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        btnsave = new javax.swing.JButton();
        btnhistory = new javax.swing.JButton();
        btndelhistory = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cbchoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbchooseActionPerformed(evt);
            }
        });

        jButton1.setText("import file");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("SPEAKING");

        jLabel2.setText("question: ");

        jLabel4.setText("Time");

        btnplay.setText("Play");
        btnplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnplayActionPerformed(evt);
            }
        });

        jLabel7.setText("choose");

        btnRecord.setText("recording");
        btnRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRecordActionPerformed(evt);
            }
        });

        btnnext.setText("Next");
        btnnext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnextActionPerformed(evt);
            }
        });

        btnback.setText("Back");
        btnback.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbackActionPerformed(evt);
            }
        });

        txtQuestion.setColumns(20);
        txtQuestion.setRows(5);
        jScrollPane1.setViewportView(txtQuestion);

        lbquestion.setText("jLabel3");

        lbtime.setText("jLabel3");

        btngroup1.add(rda);
        rda.setText("A");

        btngroup1.add(rdb);
        rdb.setText("B");

        btngroup1.add(rdc);
        rdc.setText("C");

        btngroup1.add(rdd);
        rdd.setText("D");

        jLabel3.setText("assess");

        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btnhistory.setText("history");
        btnhistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnhistoryActionPerformed(evt);
            }
        });

        btndelhistory.setText("Delete History");
        btndelhistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndelhistoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnback, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnplay, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnnext, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbtime, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbchoose, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(61, 61, 61))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbquestion)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnsave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnhistory, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rda)
                                    .addComponent(rdb)
                                    .addComponent(rdc)
                                    .addComponent(jLabel3)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(rdd)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btndelhistory)))
                                .addGap(0, 0, Short.MAX_VALUE))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbchoose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbquestion)
                    .addComponent(jLabel3))
                .addGap(1, 1, 1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rda)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdb)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdc)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rdd)
                            .addComponent(btndelhistory))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnsave)
                            .addComponent(btnhistory))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbtime, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnplay)
                    .addComponent(btnRecord)
                    .addComponent(btnnext)
                    .addComponent(btnback))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnbackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbackActionPerformed
        // TODO add your handling code here:
        btnsave.setEnabled(false);
        btnplay.setEnabled(false);
        timer.stop();
        btnRecord.setText("recording");
        int num = Integer.parseInt(lbquestion.getText());
        if (num <= 0) {
            num=0;
            txtQuestion.setText(arr[num]);

            String[] parts = arrtime[num].split(":");
            hour =    Integer.parseInt(parts[0]) ;
            min =    Integer.parseInt(parts[1]) ;
            sec =    Integer.parseInt(parts[2]) ;
            lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));

        } else{
            num = num-1;
            String a = String.valueOf(num);
            txtQuestion.setText(arr[num]);
            lbquestion.setText(a);
            String[] parts = arrtime[num].split(":");
            hour =    Integer.parseInt(parts[0]) ;
            min =    Integer.parseInt(parts[1]) ;
            sec =    Integer.parseInt(parts[2]) ;
            lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));
        }
    }//GEN-LAST:event_btnbackActionPerformed

    private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnextActionPerformed
        // TODO add your handling code here:
        btnsave.setEnabled(false);
        btnplay.setEnabled(false);
        timer.stop();
        btnRecord.setText("recording");
        int num = Integer.parseInt(lbquestion.getText());
        if (num >= arr.length-1) {
            num = arr.length-1;
            txtQuestion.setText(arr[num]);
            String[] parts = arrtime[num].split(":");
            hour =    Integer.parseInt(parts[0]) ;
            min =    Integer.parseInt(parts[1]) ;
            sec =    Integer.parseInt(parts[2]) ;
            lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));
        } else{
            num = num+1;
            String a = String.valueOf(num);
            txtQuestion.setText(arr[num]);
            lbquestion.setText(a);
            String[] parts = arrtime[num].split(":");
            hour =    Integer.parseInt(parts[0]) ;
            min =    Integer.parseInt(parts[1]) ;
            sec =    Integer.parseInt(parts[2]) ;
            lbtime.setText(String.valueOf(hour +" : " + min +" : "+sec));
        }
    }//GEN-LAST:event_btnnextActionPerformed

    private void btnRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRecordActionPerformed
        // TODO add your handling code here:
        if(btnRecord.getText().equals("recording")){
            timer.start();
            captureAudio();
            btnRecord.setText("Ok");
            btnplay.setEnabled(false);
            btnsave.setEnabled(false);
        }
        else if (btnRecord.getText().equals("Ok")) {
            timer.stop();
            btnRecord.setText("recording");
            targetDataLine.stop();
            targetDataLine.close();
            btnplay.setEnabled(true);
            btnsave.setEnabled(true);
        }

    }//GEN-LAST:event_btnRecordActionPerformed

    private void btnplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnplayActionPerformed
        // TODO add your handling code here:
        spDAO.PlayAudio();
    }//GEN-LAST:event_btnplayActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Faddspeaking r = new Faddspeaking();
        r.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void cbchooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbchooseActionPerformed
        // TODO add your handling code here:
        String filename = (String)vUrlSpeaking.get(cbchoose.getSelectedIndex());
        try {
            arr = spDAO.readUsingBufferedReader(filename, StandardCharsets.UTF_8);
            txtQuestion.setText(arr[0]);
            System.out.println(arr[0]);
            lbquestion.setText("0");
        } catch (IOException ex) {
            Logger.getLogger(Fspeaking.class.getName()).log(Level.SEVERE, null, ex);
        }
        //time
        String UrlTime = (String)vUrlTime.get(cbchoose.getSelectedIndex());
        
        try {
            arrtime = spDAO.readUsingBufferedReader(UrlTime, StandardCharsets.UTF_8);
                    
                    } catch (IOException ex) {
            Logger.getLogger(Fspeaking.class.getName()).log(Level.SEVERE, null, ex);
        }
        lbtime.setText(arrtime[0]);
    }//GEN-LAST:event_cbchooseActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        String url = (String)vUrlHistory.get(cbchoose.getSelectedIndex());
        String assess = "";
        String time = lbtime.getText();
        if (rda.isSelected() == true) {
            assess = "A";
        }else if(rdb.isSelected() == true)
               assess = "B"; 
        else if(rdc.isSelected() == true)
               assess = "C"; 
        else if(rdd.isSelected() == true)
               assess = "D"; 
        String str =" Speaking: "+lbquestion.getText()+"  |  time: "+ time+"    |   assess: "+ assess;
        System.out.println(str);
        spDAO.SaveFile(str, url);
    }//GEN-LAST:event_btnsaveActionPerformed

    private void btnhistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnhistoryActionPerformed
        // TODO add your handling code here:
        String url = (String)vUrlHistory.get(cbchoose.getSelectedIndex());
        String str = spDAO.readFile(url);
        spDAO.infoBox(str,"messages");
    }//GEN-LAST:event_btnhistoryActionPerformed

    private void btndelhistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndelhistoryActionPerformed
        // TODO add your handling code here:
        String url = (String)vUrlHistory.get(cbchoose.getSelectedIndex());
        spDAO.DeleteFile(url);
    }//GEN-LAST:event_btndelhistoryActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Fspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Fspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Fspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Fspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Fspeaking().setVisible(true);
            }
        });
    }
    
    
    private void captureAudio(){
    try{
      //Get things set up for capture
      audioFormat = getAudioFormat();
      DataLine.Info dataLineInfo =
                          new DataLine.Info(
                            TargetDataLine.class,
                            audioFormat);
      targetDataLine = (TargetDataLine)
               AudioSystem.getLine(dataLineInfo);

      //Create a thread to capture the microphone
      // data into an audio file and start the
      // thread running.  It will run until the
      // Stop button is clicked.  This method
      // will return after starting the thread.
      new CaptureThread().start();
    }catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }//end catch
  }//end captureAudio method

  //This method creates and returns an
  // AudioFormat object for a given set of format
  // parameters.  If these parameters don't work
  // well for you, try some of the other
  // allowable parameter values, which are shown
  // in comments following the declarations.
  private AudioFormat getAudioFormat(){
    float sampleRate = 8000.0F;
    //8000,11025,16000,22050,44100
    int sampleSizeInBits = 16;
    //8,16
    int channels = 1;
    //1,2
    boolean signed = true;
    //true,false
    boolean bigEndian = false;
    //true,false
    return new AudioFormat(sampleRate,
                           sampleSizeInBits,
                           channels,
                           signed,
                           bigEndian);
  }//end getAudioFormat
    
    
class CaptureThread extends Thread{
  public void run(){
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;    
    //File audioFile = new File("junk.wav");
    File audioFile = new File( "~tmp1"+"." + fileType.getExtension());

    try{
      targetDataLine.open(audioFormat);
      targetDataLine.start();
      AudioSystem.write(
            new AudioInputStream(targetDataLine),
            fileType,
            audioFile);
    }catch (Exception e){
      e.printStackTrace();
    }//end catch

  }//end run
}
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRecord;
    private javax.swing.JButton btnback;
    private javax.swing.JButton btndelhistory;
    private javax.swing.ButtonGroup btngroup1;
    private javax.swing.JButton btnhistory;
    private javax.swing.JButton btnnext;
    private javax.swing.JButton btnplay;
    private javax.swing.JButton btnsave;
    private javax.swing.JComboBox cbchoose;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbquestion;
    private javax.swing.JLabel lbtime;
    private javax.swing.JRadioButton rda;
    private javax.swing.JRadioButton rdb;
    private javax.swing.JRadioButton rdc;
    private javax.swing.JRadioButton rdd;
    private javax.swing.JTextArea txtQuestion;
    // End of variables declaration//GEN-END:variables
}
